package org.ntu.csn.doraemon.sprite;
import java.util.Random;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;

import org.ntu.csn.doraemon.model.GameObject;
import org.ntu.csn.doraemon.tool.Constant;


import org.ntu.csn.doraemon.canvas.Main;


public class Enhancers extends GameObject {

	
	
	public Enhancers(Main parent, int maxEnhancers, Image img,int picwidth,int picheight,int width,int height) {
	    
		super(img,picwidth,picheight);
		this.parent = parent;
	    enhancers=new int[maxEnhancers][6];
	    rnd=new Random();
	    this.width=width;
	    this.height=height;
	}

  public void initEnhancers(){
    for (int i = 0; i < enhancers.length; i++) {
      initEnhancer(i);
    }
  }

  private void initEnhancer(int i) {
    enhancers[i][TYPE] = Constant.BULLET_TYPE_TOP; //(rnd.nextInt() & 0x7fffffff) % 4; //type
    enhancers[i][ALIVE] = 1; //alive
    switch (enhancers[i][TYPE]) {

      case Constant.BULLET_TYPE_TOP:
        enhancers[i][X] = (rnd.nextInt() & 0x7fffffff) % width;
        enhancers[i][Y] = (rnd.nextInt() & 0x7fffffff) % (height/6); //-5;
        enhancers[i][VX] = 0; //Velocity for X
        enhancers[i][VY] = (rnd.nextInt() & 0x7fffffff) % 3 + 1; //Velocity for Y
        break;

    }
  }


  private void paint(Graphics g,int i){
    updataspritepos(i);
    sprite.paint(g);
  }

  public void paint(Graphics g) {
    for (int i = 0; i < enhancers.length; i++) {
      if(enhancers[i][ALIVE]==0){
        continue;
      }
      sprite.setPosition(enhancers[i][X],enhancers[i][Y]);
      //parent.getManager().insert(sprite, 0);
      sprite.paint(g);
    }
  }

  public void checkForCollision(Sprite collisionSprite, boolean needcollision){
    for (int i = 0; i < enhancers.length; i++) {
      if(enhancers[i][ALIVE]==0){
        continue;
      }
      if(needcollision){
        //System.out.println("needcollision ");
        if (isCollision(collisionSprite, i, Constant.COLLISION_RADIUS_DORAEMON)) {
          //System.out.println("collision ");
        	enhancers[i][ALIVE] = 0;
        	//initEnhancer(i);
        	enhancersHit++;
			parent.setDx(parent.getDx() + 2 );
			
        	continue;
        }
      }
      updata(i);
    }
  }
  public void updata(int i){
    enhancers[i][1]+=enhancers[i][3];
    enhancers[i][2]+=enhancers[i][4];
    if(enhancers[i][1]<-5 || enhancers[i][1]>width+5){
      //enhancers[i][3]*=-1;
    	enhancers[i][ALIVE] = 0;
    }
    if(enhancers[i][2]<-5 || enhancers[i][2]>height+5){
      // enhancers[i][4]*=-1;
    	enhancers[i][ALIVE] = 0;
    }
  }


  private boolean isCollision(Sprite sprite,int i,int range){
    //updataspritepos(i);
    //return sprite.collidesWith(this.sprite,true);
    boolean result=false;
    int planeXCenter=sprite.getX()+12;
    int planeYCenter=sprite.getY()+12;
    int bulletXCenter=enhancers[i][1]+3;
    int bulletYCenter=enhancers[i][2]+3;
    if(Math.abs(planeXCenter-bulletXCenter) < range){
      if (Math.abs(planeYCenter - bulletYCenter )< range) {
        result = true;
      }
    }
    return result;
  }

  private void updataspritepos(int i){
    sprite.setPosition(enhancers[i][1],enhancers[i][2]);
  }
  
	private int[][] enhancers;
	
	private int enhancerTotal;
	private Random rnd;
	private int width,height;

	private int TYPE = 0;
	private int X = 1;
	private int Y = 2;
	private int VX = 3;
	private int VY = 4;
	private int ALIVE = 5;
	
	private int enhancersHit = 0;
	private Main parent;
}